﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace Medicalsystem.MCP.FormDesigner.Ctl
{
    /// <summary>
    /// 定位针控件 -用于显示或隐藏操作
    /// </summary>
    public class ctl_HeaderPin : System.Windows.Forms.Control
    {
        private bool _autoHide = true;

        /// <summary>
        /// 是否自动隐藏
        /// </summary>
        public bool AutoHide
        {
            get
            {
                return _autoHide;
            }
            set
            {
                _autoHide = value;
                this.Refresh();
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="pe"></param>
        protected override void OnPaint(PaintEventArgs pe)
        {
            // TODO: 某些操作系统不支持系统定制样式 ， 不支持的系统采用手绘

            if (_autoHide)
            {
                try
                {
                    if (VisualStyleRenderer.IsElementDefined(VisualStyleElement.ExplorerBar.HeaderPin.Normal))
                    {
                        VisualStyleRenderer renderer2 =
                             new VisualStyleRenderer(VisualStyleElement.ExplorerBar.HeaderPin.Normal);
                        Rectangle rectangle2 = new Rectangle(0, 0, Math.Abs(this.Width), Math.Abs(this.Height));
                        renderer2.DrawBackground(pe.Graphics, rectangle2);
                    }
                }
                catch
                {
                    Pen pen = new Pen(Color.Black, 1);

                    Point Fist = new Point(Math.Abs(this.Width / 2 - 2), 5);
                    Point Center = new Point(Fist.X + 2, Math.Abs(Fist.Y + this.Height / 2 - 2));

                    pe.Graphics.DrawRectangle(pen, Fist.X, Fist.Y, 4, Math.Abs(this.Height / 2 - 2));
                    pe.Graphics.DrawLine(pen, Center, new Point(Center.X, Center.Y + 4));
                    pe.Graphics.DrawLine(pen, new Point(Math.Abs(Center.X - 3), Center.Y), new Point(Center.X + 3, Center.Y));
                    pe.Graphics.DrawLine(pen, new Point(Fist.X + 3, Fist.Y), new Point(Fist.X + 3, Fist.Y + Math.Abs(this.Height / 2 - 2)));

                    base.OnPaint(pe);
                }
            }
            else
            {
                try
                {
                    if (VisualStyleRenderer.IsElementDefined(VisualStyleElement.ExplorerBar.HeaderPin.SelectedNormal))
                    {
                        VisualStyleRenderer renderer2 =
                             new VisualStyleRenderer(VisualStyleElement.ExplorerBar.HeaderPin.SelectedNormal);
                        Rectangle rectangle2 = new Rectangle(0, 0, Math.Abs(this.Width), Math.Abs(this.Height));
                        renderer2.DrawBackground(pe.Graphics, rectangle2);
                    }
                }
                catch
                {
                    Pen pen = new Pen(Color.Black, 1);

                    Point Fist = new Point(5, this.Height / 2);
                    Point Second = new Point(Fist.X + 4, this.Height / 2);

                    pe.Graphics.DrawLine(pen, Fist, Second);
                    pe.Graphics.DrawLine(pen, new Point(Second.X, Math.Abs(Second.Y - 3)), new Point(Second.X, Second.Y + 3));
                    pe.Graphics.DrawLine(pen, new Point(Second.X, Second.Y + 1), new Point(Second.X + Math.Abs(this.Width / 2 - 2), Second.Y + 1));
                    pe.Graphics.DrawRectangle(pen, Second.X, Math.Abs(Second.Y - 2), Math.Abs(this.Width / 2 - 2), 4);

                    base.OnPaint(pe);
                }
            }
        }
    }
}